package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.DefendTypeMapper;
import com.xidian.zhiancloud.dao.SysUserAbandonMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.model.DefendType;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.DefendTypeQO;
import com.xidian.zhiancloud.vo.equipFacility.DefendTypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 维护类型设置
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@Service
public class DefendTypeService extends ServiceImpl<DefendTypeMapper, DefendType> implements IService<DefendType> {

    @Autowired
    private DefendTypeMapper defendTypeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<DefendTypeVO> selectByQO(DefendTypeQO defendTypeQO) {
        Page<DefendType> page = new Page<>(defendTypeQO.getCurrent(), defendTypeQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("add_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<DefendType> lambdaQueryWrapper = Wrappers.<DefendType>lambdaQuery();
        lambdaQueryWrapper.eq(defendTypeQO.getCompanyId() != null, DefendType::getCompanyId, defendTypeQO.getCompanyId());
        Page<DefendType> tmpAns = defendTypeMapper.selectPage(page, lambdaQueryWrapper);
        return tmpAns.convert(u -> {
            DefendTypeVO vo = new DefendTypeVO();
            BeanUtils.copyProperties(u, vo);
            SysUser user = sysUserMapper.selectById(vo.getCreatorId());
            vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
            return vo;
        });
    }
}
